/*
* Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id: serverlistview.cpp 284 2009-01-12 20:18:50Z jpnurmi $
*/

#include "serverlistview.h"
#include "serverlistmodel.h"
#include "serverfiltermodel.h"
#include <QContextMenuEvent>
#include <QHeaderView>
#include <QMenu>

ServerListView::ServerListView(QWidget* parent)
    : QTreeView(parent)
{
    d.serverModel = new ServerListModel(this);
    d.filterModel = new ServerFilterModel(this);

    d.filterModel->setSourceModel(d.serverModel);
    setModel(d.filterModel);

    header()->setStretchLastSection(false);
    header()->setResizeMode(QHeaderView::ResizeToContents);
    header()->setResizeMode(ServerListModel::ColumnName, QHeaderView::Stretch);
}

Server ServerListView::server(const QModelIndex& index) const
{
    QVariant data = index.data(ServerListModel::ServerRole);
    return data.value<Server>();
}

ServerList ServerListView::servers() const
{
    return d.serverModel->servers();
}

void ServerListView::addServers(const ServerList& servers)
{
    d.serverModel->addServers(servers);
}

Server ServerListView::currentServer() const
{
    QModelIndex index = currentIndex();
    QVariant data = index.data(ServerListModel::ServerRole);
    return data.value<Server>();
}

ServerList ServerListView::selectedServers() const
{
    ServerList servers;
    QModelIndexList indexes = selectionModel()->selectedRows();
    foreach (QModelIndex index, indexes)
        servers += index.data(ServerListModel::ServerRole).value<Server>();
    return servers;
}

void ServerListView::saveCurrentServer(const Server& server)
{
    model()->setData(currentIndex(), server, ServerListModel::ServerRole);
}

void ServerListView::deleteServers()
{
    while (!selectionModel()->selectedIndexes().isEmpty())
    {
        QModelIndex index = selectionModel()->selectedIndexes().first();
        model()->removeRow(index.row(), index.parent());
    }
}

void ServerListView::setFilter(const QString& filter)
{
    d.filterModel->setFilterWildcard(filter);
}

void ServerListView::contextMenuEvent(QContextMenuEvent* event)
{
    QModelIndex index = indexAt(event->pos());
    if (!selectionModel()->isSelected(index))
    {
        QItemSelectionModel::SelectionFlags flags = selectionCommand(index);
        selectionModel()->select(index, flags);
    }

    QMenu menu(this);
    menu.addActions(actions());
    menu.exec(event->globalPos());
}

void ServerListView::dragMoveEvent(QDragMoveEvent* event)
{
    QModelIndex index = indexAt(event->pos());
    if (index.isValid())
        QTreeView::dragMoveEvent(event);
    else
        event->ignore();
}
